﻿//----------------------------------------------------------------------------- 
// <copyright file="SR7LinkFunc.cs" company="SSZN">
//	 Copyright (c) 2021 SSZN.  All rights reserved.
// </copyright>                       
// Author:Xiaobin Wang                
// Data:2020-02-20                   
// Version: SsznCam Ver 1.0.15        
// Description:  详见doc.md           
//                                    
//----------------------------------------------------------------------------- 
using BZ.Core;
using DllSampleAll;
using HalconDotNet;
using SRDLL;
using System;
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;

namespace NDK.AcqDevice
{

    //Flow
    //CallBack Mode: init(控制器编号 0/1/2等) -- connect(ip) -- start -- > 
    //               硬触发 等待IO触发 / 软触发 softTrigOne 
    //               外部判断bBatchFinish --> 取HeightData数据 -- > stop -- > disconnect -- > uninit
    //               第二张: 硬触发 等待IO触发/ 软触发 softTrigOne
    //Block Mode : init -- > connect --> startBlock(0软触发 1硬触发) -->
    //             硬触发:等IO触发/软触发:无操作 --> 
    //             取 HeightData 数据区 -->stop -->disconnect -->uninit
    //             第二张:继续 startBlock 
    public class SRController : ILineLaser
    {
        /// <summary>
        /// 设置镭射的ID
        /// </summary>
        /// <param name="deviceId">镭射ID 区间 0···3</param>
        private HImage ImageH = new HImage();
        private HImage ImageG = new HImage();
        private AutoResetEvent heightImageEvent = new AutoResetEvent(false);
        private AutoResetEvent luminanceImageEvent = new AutoResetEvent(false);
        public ConcurrentQueue<HImage> CQueneHeightImage { get; set; }
        #region Field 
        //Cam Id : 0 1 2 3 多个控制器时的标识号，从0开始
        int _currentDeviceId = 0;
        // 0 un finish 1 finish callback
        public int bBatchFinish = 0;
        String str_Err = ""; //错误信息
        #region NoUse

        //public int BatchPoint = 0;      //批处理行数
        //public int DataWidth = 3200;    //轮廓宽度
        //double m_XPitch = 0.02;         //X方向间距
        //double heightRange = 8.4;       //高度显示范围

        //int m_IOTrigger = 0;            //触发方式标志位 0 soft 1 hard
        //const int MaxColRool = 1000;    //有限循环单次获取最大行数

        #endregion NoUse
        #endregion Field

        #region Buffer Data
        public int[][] HeightData = new int[2][] { null, null };       //高度数据缓存
        public byte[][] GrayData = new byte[2][] { null, null };       //灰度数据缓存
        public int[][] EncoderData = new int[2][] { null, null };      //编码器数据缓存
        #endregion Buffer Data
        public int currentDeviceId
        {
            set
            {
                if (value <= 6 || value >= 0) //3
                {
                    _currentDeviceId = value;
                }
                else
                {
                    _currentDeviceId = 0;
                }
            }
            get { return _currentDeviceId; }
        }
        #region Func
        public HImage HeightImage(int timeout = 30000)
        {
            if (heightImageEvent.WaitOne(timeout) == true)
            {
                if (ImageH.IsInitialized())
                {
                    return ImageH.CopyImage();
                }
            }
            return null;
        }
        public HImage LuminanceImage(int timeout = 30000)
        {
            if (luminanceImageEvent.WaitOne(timeout) == true)
            {
                if (ImageG.IsInitialized())
                {
                    return ImageG.CopyImage();
                }
            }
            return null;
        }
        /// <summary>
        /// 停止采集高度图，当长时间没有高度图像返回时，可调用此方法结束等待
        /// </summary>
        /// <returns></returns>
        public bool StopAcqHeightImage()
        {
            heightImageEvent.Set();
            return Stop();
        }
        /// <summary>
        /// 停止采集亮度图，当长时间没有亮度图像返回时，可调用此方法结束等待
        /// </summary>
        /// <returns></returns>
        public bool StopAcqLuminanceImage()
        {
            luminanceImageEvent.Set();
            return Stop();
        }
        public bool IsConnected
        {
            get
            {
                int RT = SR7LinkFunc.SR7IF_GetOnlineCameraB(currentDeviceId);
                if (RT == 0)
                    return true;
                else
                    return false;
            }
        }
        public int Init()
        {

            try
            {
                if (currentDeviceId < 0 || currentDeviceId > 6) //3
                {
                    return -2;
                }
                for (int index = 0; index < 2; index++)
                {

                    HeightData[index] = new int[15000 * 3200];
                    GrayData[index] = new byte[15000 * 3200];
                    EncoderData[index] = new int[15000];

                    for (int i = 0; i < HeightData[index].Length; i++)
                    {
                        HeightData[index][i] = -1000000000;
                    }

                    for (int i = 0; i < GrayData[index].Length; i++)
                    {
                        GrayData[index][i] = byte.MinValue;
                    }

                    for (int i = 0; i < EncoderData[index].Length; i++)
                    {
                        EncoderData[index][i] = int.MinValue;
                    }

                }

            }
            catch (Exception e)
            {
                str_Err = "内存申请失败 " + e.ToString();
                return -1;
            }

            return 0;

        }
        public int Uninit()
        {
            //释放内存资源??

            return 0;
        }
        public bool SwitchJob(string name)
        {
            if (ChangeActiveProgram(int.Parse(name)))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public void LMIChangeScanLength(double length)
        {
            return;
        }

        #region 回调batchOneTimeCallBack
        //ip: sample 192.168.0.10
        //runMode : 0 回调batchOneTimeCallBack 1 阻塞 
        public bool Connect(string ip, int portID)
        {
            int times = 0;
            currentDeviceId = portID;
            if (IsConnected)
            {
                DisConnect();
            }
        reconnect:
            times++;
            int runMode = 0;
            if (Init() != 0)
            {
                return false;
            }
            try
            {
                SR7IF_ETHERNET_CONFIG _ethernetConfig;
                string[] ipTmp = ip.Split('.');
                if (ipTmp.Length != 4)
                {
                    //Ip Num Is Err.
                    str_Err = "Ip Num Is Err.";
                    return false;//-2;
                }
                _ethernetConfig.abyIpAddress = new Byte[]
                {
                    Convert.ToByte(ipTmp[0]),
                    Convert.ToByte(ipTmp[1]),
                    Convert.ToByte(ipTmp[2]),
                    Convert.ToByte(ipTmp[3])
                };
                int errO = SR7LinkFunc.SR7IF_EthernetOpen(currentDeviceId, ref _ethernetConfig);
                if (errO != 0)
                {
                    if (times < 2)
                    {
                        try
                        {
                            DisConnect();
                        }
                        catch (Exception)
                        {
                        }
                        goto reconnect;
                    }
                    str_Err = "SR7IF_EthernetOpen Err.";
                    return false;
                }

                if (runMode == 0)
                {
                    batchOneTimeCallBack = new SR7IF_BatchOneTimeCallBack(BatchOneTimeCallBack);
                    int reT = SR7LinkFunc.SR7IF_SetBatchOneTimeDataHandler(currentDeviceId, batchOneTimeCallBack);
                    if (reT != 0)
                    {
                        return false;
                    }
                }

            }
            catch (Exception e)
            {
                str_Err = "connect Err." + e.ToString();
                return false;
            }
            return true;
        }
        #endregion
        public void DisConnect()
        {
            //批处理停止
            int Rc = SR7LinkFunc.SR7IF_StopMeasure(currentDeviceId);
            //if (Rc != 0)
            //    throw new LaserException("停止批处理失败,返回值" + Rc);

            //关闭设备
            int reT = SR7LinkFunc.SR7IF_CommClose(currentDeviceId);
            //if (reT < 0)
            //    throw new LaserException("设备关闭失败,返回值" + reT);
        }
        //即接收软触发,也接收硬触发
        public bool Start()
        {
            try
            {
                heightImageEvent.Reset();
                luminanceImageEvent.Reset();
                int errS = SR7LinkFunc.SR7IF_StartMeasureWithCallback(currentDeviceId, 0);
                //1 回调需外部触发 ，0 直接回调
                if (errS != 0)
                {
                    str_Err = "开始批处理失败";
                    return false;
                }
            }
            catch (Exception ex)
            {
                str_Err = "start Err." + ex.Message;
                return false;
            }

            return true;

        }
        public bool Stop()
        {
            try
            {
                int errS = SR7LinkFunc.SR7IF_StopMeasure(currentDeviceId);

                if (errS != 0)
                {
                    str_Err = "SR7IF_StopMeasure Err";
                    return false;
                }
            }
            catch (Exception ex)
            {
                str_Err = "stop Err." + ex.Message;
                return false;
            }

            return true;
        }
        /// <summary>
        /// 软件界面从1开始，但是实际从0开始
        /// </summary>
        /// <param name="ProgramNo"></param>
        /// <returns></returns>
        public bool ChangeActiveProgram(int ProgramNo)
        {
            int ret = SR7LinkFunc.SR7IF_SwitchProgram(currentDeviceId, ProgramNo);
            if (ret != 0)
                return false;
            else
                return true;
        }
        #region B镭射头使用方法，大多无意义，仅有HeightImageB和LuminanceImageB
        public bool ConnectB(string ip)
        {
            return true;
        }
        public bool StartB()
        {
            return true;
        }
        public bool StopB()
        {
            return true;
        }

        public HImage HeightImageB(int timeout = 30000)
        {
            if (heightImageEvent.WaitOne(timeout) == true)
            {
                if (ImageH.IsInitialized())
                {
                    return ImageH.CopyImage();
                }
            }
            return null;
        }
        public HImage LuminanceImageB(int timeout = 30000)
        {
            if (luminanceImageEvent.WaitOne(timeout) == true)
            {
                if (ImageG.IsInitialized())
                {
                    return ImageG.CopyImage();
                }
            }
            return null;
        }

        #endregion
        public bool SoftTrigOne()
        {
            int errT = SR7LinkFunc.SR7IF_TriggerOneBatch(currentDeviceId);

            if (errT != 0)
            {
                str_Err = "软件触发失败";
                return false;
            }

            return true;

        }
        public int startBlock(int ioTrig)
        {
            try
            {
                Trace.WriteLine("startBlock Enter.");

                int errS = (ioTrig == 0) ?
                SR7LinkFunc.SR7IF_StartMeasure(currentDeviceId, 20000)
                :
                SR7LinkFunc.SR7IF_StartIOTriggerMeasure(currentDeviceId, 20000, 0);
                if (errS != 0) { return errS; }

                Trace.WriteLine("Start ReceiveData .");

                // 接收数据
                IntPtr DataObject = new IntPtr();
                int errR = SR7LinkFunc.SR7IF_ReceiveData(currentDeviceId, DataObject);
                if (0 != errR)
                {
                    str_Err = ("ReceiveData Fail.");
                    SR7LinkFunc.SR7IF_StopMeasure(currentDeviceId);
                    return errR;
                }

                using (PinnedObject pin = new PinnedObject(HeightData[0]))//内存自动释放接口
                {
                    int Rc = SR7LinkFunc.SR7IF_GetProfileData(currentDeviceId, new IntPtr(), pin.Pointer);   // pin.Pointer 获取高度数据缓存地址
                    Trace.WriteLine("高度数据获取" + ((Rc == 0) ? "成功" : "失败"));
                }

                int RcIn = 0;
                // 获取亮度数据
                using (PinnedObject pin = new PinnedObject(GrayData[0]))       //内存自动释放接口
                {
                    RcIn = SR7LinkFunc.SR7IF_GetIntensityData(currentDeviceId, new IntPtr(), pin.Pointer);
                    Trace.WriteLine("灰度数据获取" + ((RcIn == 0) ? "成功" : "失败"));
                }

                //获取编码器值
                using (PinnedObject pin = new PinnedObject(EncoderData[0]))       //内存自动释放接口
                {
                    int Rc = SR7LinkFunc.SR7IF_GetEncoder(currentDeviceId, new IntPtr(), pin.Pointer);
                    Trace.WriteLine("编码器值获取" + ((Rc == 0) ? "成功" : "失败"));
                }

                //
                int iCamB = SR7LinkFunc.SR7IF_GetOnlineCameraB(currentDeviceId);

                //待测试
                //拆分buffer区
                if (iCamB == 0)
                {

                    int BatchPoint = SR7LinkFunc.SR7IF_ProfilePointSetCount(currentDeviceId, new IntPtr());
                    int BatchWidth = SR7LinkFunc.SR7IF_ProfileDataWidth(currentDeviceId, new IntPtr());

                    for (int i = 0; i < BatchPoint; i++)
                    {
                        Array.Copy(HeightData[0], (2 * i + 1) * BatchWidth,
                                   HeightData[1], i * BatchWidth, BatchWidth);
                        Array.Copy(HeightData[0], (2 * i) * BatchWidth,
                                   HeightData[0], i * BatchWidth, BatchWidth);
                    }

                    if (RcIn == 0)
                    {
                        for (int i = 0; i < BatchPoint; i++)
                        {
                            Array.Copy(GrayData[0], (2 * i + 1) * BatchWidth,
                                       GrayData[1], i * BatchWidth, BatchWidth);
                            Array.Copy(GrayData[0], (2 * i) * BatchWidth,
                                       GrayData[0], i * BatchWidth, BatchWidth);
                        }
                    }

                }

            }
            catch (Exception ex)
            {
                str_Err = "start Err." + ex.Message;
                return -1;
            }

            return 0;

        }

        #region Callback 回调方法     // Once Callback
        private void BatchOneTimeCallBack(IntPtr info, IntPtr data)
        {
            //  Trace.WriteLine("BatchOneTimeCallBack!");
            try
            {
                SR7IF_STR_CALLBACK_INFO coninfo = new SR7IF_STR_CALLBACK_INFO();
                coninfo = (SR7IF_STR_CALLBACK_INFO)Marshal.PtrToStructure(info, typeof(SR7IF_STR_CALLBACK_INFO));

                if (coninfo.returnStatus == -100)
                    return;
                ImageH = new HImage();
                ImageG = new HImage();
                int mBatchPoint = coninfo.BatchPoints;
                int mBatchWidth = coninfo.xPoints;

                IntPtr[] mTmpData = new IntPtr[2];
                IntPtr[] mTmpGraydata = new IntPtr[2];
                IntPtr[] mTmpEncoderdata = new IntPtr[2];

                for (int index = 0; index < coninfo.HeadNumber; index++)
                {
                    mTmpData[index] = SR7LinkFunc.SR7IF_GetBatchProfilePoint(data, index);
                    mTmpGraydata[index] = SR7LinkFunc.SR7IF_GetBatchIntensityPoint(data, index);
                    mTmpEncoderdata[index] = SR7LinkFunc.SR7IF_GetBatchEncoderPoint(data, index);

                    if (mTmpData[index] != IntPtr.Zero)
                    {
                        // Marshal.Copy(mTmpData[index], HeightData[index], 0, mBatchPoint * mBatchWidth);
                        HImage ImageH1 = new HImage();
                        ImageH1.GenImage1("int4", mBatchWidth, mBatchPoint, mTmpData[index]);
                        //   ImageH.WriteImage("tiff", 0, "D:\\23.tiff");
                        ImageH1 = ImageH1.ConvertImageType("real");
                        if (index == 0)
                        {
                            ImageH = ImageH1.ScaleImage(1 / 100000.0, 0);
                        }
                        else
                        {
                            // ImageH2 = ImageH1.ScaleImage(1 / 100000.0, 0);
                        }
                    }
                    else
                    {
                        Trace.WriteLine("内存不足,相机头A高度数据获取失败");
                    }
                    if (mTmpGraydata[index] != IntPtr.Zero)
                    {
                        //  Marshal.Copy(mTmpGraydata[index], GrayData[index], 0, mBatchPoint * mBatchWidth);
                        if (index == 0)
                        {
                            ImageG.GenImage1("byte", mBatchWidth, mBatchPoint, mTmpGraydata[index]);
                        }
                        else
                        {
                            //ImageG2.GenImage1("byte", mBatchWidth, mBatchPoint, mTmpGraydata[index]);
                        }
                    }
                    else
                    {
                        Trace.WriteLine("内存不足,相机头A灰度数据获取失败");
                    }
                    if (mTmpEncoderdata[index] != IntPtr.Zero)
                    {
                        Marshal.Copy(mTmpEncoderdata[index], EncoderData[index], 0, mBatchPoint);
                    }
                    else
                    {
                        Trace.WriteLine("内存不足,相机头A编码器获取失败");
                    }

                }
                bBatchFinish = 1;

            }
            catch
            {

            }
            heightImageEvent.Set();
            luminanceImageEvent.Set();
        }
        SR7IF_BatchOneTimeCallBack batchOneTimeCallBack;
        #endregion Callback

        #region SSZN 备用方法
        //iAB A cam 0 B cam 1 配置序号 1- 64 
        public int setParam(int iAB, int configNum, int SupportItem, int num)
        {
            try
            {
                int depth = 0x02;//1 掉电不保存;2 掉电保存

                int Category = 0x00;//不同页面 0 1 2 
                int Item = 0x01;
                int[] tar = new int[4] { iAB, 0, 0, 0 };

                int DataSize = 0;
                int errT = TransCategory(SupportItem, out Category, out Item, out DataSize);

                if (errT != 0)
                {
                    str_Err = "TransCategory Err.";
                    return errT;
                }

                byte[] pData = null;
                int errN = TransNum(num, DataSize, ref pData);
                if (errN != 0)
                {
                    str_Err = "TransNum Err.";
                    return errN;
                }

                using (PinnedObject pin = new PinnedObject(pData))
                {
                    int errS = SR7LinkFunc.SR7IF_SetSetting((uint)currentDeviceId, depth, 15 + configNum,
                        Category, Item, tar, pin.Pointer, DataSize);
                    if (errS != 0)
                    {
                        return errS;
                    }

                }

            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.Message);
                str_Err = "setParam Err. " + ex.Message;
                return -1;
            }

            return 0;

        }
        public int getParam(int iAB, int configNum, int SupportItem, out int num)
        {
            num = 0;

            try
            {

                if (SupportItem <= 0x3000)
                {

                    int Category = 0x00;//不同页面 0 1 2 
                    int Item = 0x01;
                    int[] tar = new int[4] { iAB, 0, 0, 0 };

                    int DataSize = 1;

                    int errT = TransCategory(SupportItem, out Category, out Item, out DataSize);

                    if (0 != errT)
                    {
                        return errT;
                    }

                    byte[] pData = new byte[DataSize];

                    using (PinnedObject pin = new PinnedObject(pData))
                    {

                        int errG = SR7LinkFunc.SR7IF_GetSetting((uint)currentDeviceId, 15 + configNum,
                             Category, Item, tar, pin.Pointer, DataSize);

                        if (0 != errG)
                        {
                            return errG;
                        }

                        StringBuilder sb = new StringBuilder();

                        //Get Data
                        for (int i = 0; i < pData.Length; i++)
                        {
                            num += pData[i] * (int)Math.Pow(256, i);
                        }

                    }

                }
                else if (SupportItem <= 0x4000 && SupportItem > 0x3000)
                {
                    switch (SupportItem)
                    {
                        //X数据宽度(单位像素)
                        case (int)SR7IF_SETTING_ITEM.X_PIXEL:
                            int camNum = SR7LinkFunc.SR7IF_GetOnlineCameraB(currentDeviceId) == 0 ? 2 : 1;
                            num = SR7LinkFunc.SR7IF_ProfileDataWidth(currentDeviceId, new IntPtr()) / camNum;

                            break;
                        default:
                            return -3;
                    }

                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine("getParam Err." + ex.Message);
                str_Err = "getParam Err." + ex.Message;
                return -1;
            }

            return 0;

        }
        public int getParam(int iAB, int configNum, int SupportItem, out double num)
        {
            num = 0.0;
            try
            {
                switch (SupportItem)
                {
                    //X Resolution  
                    case (int)SR7IF_SETTING_ITEM.X_PITCH:
                        num = SR7LinkFunc.SR7IF_ProfileData_XPitch(currentDeviceId, new IntPtr());
                        break;

                    default:

                        return -2;
                }
            }
            catch (Exception ex)
            {
                str_Err = "getParam Err. " + ex.Message;
                return -1;
            }

            return 0;

        }
        //返回指定端口0 - 7 输入电平，0/False 低 1/True 高
        public int GetIo(uint port, out bool level)
        {
            level = false;

            try
            {
                using (PinnedObject pin = new PinnedObject(level))
                {
                    int errG = SR7LinkFunc.SR7IF_GetInputPortLevel((uint)currentDeviceId, port, pin.Pointer);

                    if (0 != errG)
                    {
                        return errG;
                    }
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine("GetIo:" + ex.Message);
                return -1;
            }

            return 0;

        }
        public int SetIo(uint port, bool level)
        {
            try
            {
                int errS = SR7LinkFunc.SR7IF_SetOutputPortLevel((uint)currentDeviceId, port, level);

                if (0 != errS)
                {
                    return errS;
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine("SetIo:" + ex.Message);
                return -1;
            }

            return 0;

        }
        private int TransNum(int num, int DataSize, ref byte[] byteNum)
        {
            try
            {
                byteNum = new byte[DataSize];

                for (int i = 0; i < DataSize; i++)
                {
                    byteNum[i] = (byte)((num >> (i * 8)) & 0xFF);
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine("TransNum Err. " + ex.Message);
                str_Err = "TransNum Err. " + ex.Message;
                return -1;
            }

            return 0;

        }
        private int TransCategory(int SupportItem, out int Category, out int Item, out int DataSize)
        {
            Category = SupportItem / 256;
            Item = SupportItem % 256;
            DataSize = 1;

            try
            {

                switch (SupportItem)
                {
                    //触发模式
                    case (int)SR7IF_SETTING_ITEM.TRIG_MODE:
                        DataSize = 1;
                        break;

                    //采样周期
                    case (int)SR7IF_SETTING_ITEM.SAMPLED_CYCLE:
                        DataSize = 4;
                        break;

                    //批处理开关
                    case (int)SR7IF_SETTING_ITEM.BATCH_ON_OFF:
                        DataSize = 1;
                        break;

                    //编码器类型
                    case (int)SR7IF_SETTING_ITEM.ENCODER_TYPE:
                        DataSize = 1;
                        break;

                    //细化点数
                    case (int)SR7IF_SETTING_ITEM.REFINING_POINTS:
                        DataSize = 2;
                        break;

                    //批处理点数
                    case (int)SR7IF_SETTING_ITEM.BATCH_POINT:

                        DataSize = 2;
                        break;

                    case (int)SR7IF_SETTING_ITEM.CYCLICAL_PATTERN:
                        DataSize = 1;
                        break;

                    case (int)SR7IF_SETTING_ITEM.Z_MEASURING_RANGE://????
                        DataSize = 1;
                        break;

                    //感光灵敏度
                    case (int)SR7IF_SETTING_ITEM.SENSITIVITY:
                        DataSize = 1;
                        break;

                    //曝光时间
                    case (int)SR7IF_SETTING_ITEM.EXP_TIME:
                        DataSize = 1;
                        break;

                    //光亮控制
                    case (int)SR7IF_SETTING_ITEM.LIGHT_CONTROL:
                        DataSize = 1;
                        break;

                    //激光亮度上限
                    case (int)SR7IF_SETTING_ITEM.LIGHT_MAX:
                        DataSize = 1;
                        break;

                    //激光亮度下限
                    case (int)SR7IF_SETTING_ITEM.LIGHT_MIN:
                        DataSize = 1;
                        break;

                    //峰值灵敏度
                    case (int)SR7IF_SETTING_ITEM.PEAK_SENSITIVITY:
                        DataSize = 1;
                        break;

                    //峰值选择
                    case (int)SR7IF_SETTING_ITEM.PEAK_SELECT:
                        DataSize = 1;
                        break;

                    //X轴压缩设定
                    case (int)SR7IF_SETTING_ITEM.X_SAMPLING:
                        DataSize = 1;
                        break;

                    //X轴中位数滤波
                    case (int)SR7IF_SETTING_ITEM.FILTER_X_MEDIAN:
                        DataSize = 1;
                        break;

                    //Y轴中位数滤波
                    case (int)SR7IF_SETTING_ITEM.FILTER_Y_MEDIAN:
                        DataSize = 1;
                        break;

                    //X轴平滑滤波
                    case (int)SR7IF_SETTING_ITEM.FILTER_X_SMOOTH:
                        DataSize = 1;
                        break;

                    //Y轴平滑滤波
                    case (int)SR7IF_SETTING_ITEM.FILTER_Y_SMOOTH:
                        DataSize = 2;
                        break;

                    //3D/2.5D模式切换
                    case (int)SR7IF_SETTING_ITEM.CHANGE_3D_25D:
                        DataSize = 1;
                        break;

                    default:
                        Item = 0;
                        DataSize = 1;
                        return -1;

                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine("TransCategory Err. " + ex.Message);
                str_Err = "TransCategory Err. " + ex.Message;
                return -2;
            }

            return 0;

        }
        public int getErrStr(out string strErr)
        {
            strErr = str_Err;
            return 0;
        }
        public int getCamBOnline(out int num)
        {
            num = (SR7LinkFunc.SR7IF_GetOnlineCameraB(currentDeviceId) == 0) ? 2 : 1;
            return 0;
        }

        //返回的高度height 是FS的一半
        public int getCamHeight(out double height)
        {
            height = 0.0;

            try
            {
                //获取型号判断高度范围
                IntPtr str_Model = SR7LinkFunc.SR7IF_GetModels(currentDeviceId);
                String s_model = Marshal.PtrToStringAnsi(str_Model);

                switch (s_model)
                {
                    case "SR7050": height = 2.5; break;
                    case "SR7080": height = 6.0; break;
                    case "SR7140": height = 12.0; break;
                    case "SR7240": height = 20.0; break;
                    case "SR7400": height = 100.0; break;
                    case "SR7300": height = 144.0; break;
                    case "SR7900": height = 250.0; break;
                    case "SR71600": height = 750.0; break;

                    case "SR7060D": height = 3.0; break;//????

                    case "SR8020": height = 2.6; break;
                    case "SR8060": height = 9.0; break;

                    case "SR6030": height = 5.0; break;
                    case "SR6070": height = 13.5; break;
                    case "SR6071": height = 35.5; break;
                    case "SR6130": height = 70.0; break;
                    case "SR6260": height = 145.0; break;

                    default:

                        return -2;
                }

            }
            catch (Exception ex)
            {

                return -1;
            }

            return 0;

        }
        public int getCurEncoderValue(out UInt32 encodeVal)
        {
            int err = 0;
            encodeVal = 0;

            UInt32[] pData = new UInt32[1] { 0 };

            using (PinnedObject pin = new PinnedObject(pData))
            {
                err = SR7LinkFunc.SR7IF_GetCurrentEncoder(currentDeviceId, pin.Pointer);
            }

            Trace.WriteLine(pData[0]);

            //if (err != 0)
            //{
            //    return err;
            //}

            encodeVal = pData[0];

            return 0;

        }
        #endregion 

        #endregion Func
    }
}
